<?php

namespace app\admin\controller;

use think\Controller;
use think\Image;

class Upload extends Controller
{
    protected $root='public/uploads/';
    /**
     * 图片缩略图
     */
    public function image_thumb()
    {

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('the_files');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->rule('uniqid')->move($this->root.'image', true, false);
        if ($info) {
            $image = Image::open($this->root.'image/' . $info->getSaveName());
//            $image->thumb('800','800')->save($this->root.'image/lg_'.$info->getSaveName());
            $image->thumb('500', '500')->save($this->root.'image/md_' . $info->getSaveName());
            $image->thumb('150', '150')->save($this->root.'image/sm_' . $info->getSaveName());
            echo $info->getSaveName();
        } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }

    /**
     * 图片
     * 上传png格式没有缩略功能
     */
    public function image()
    {

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('the_files');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->rule('uniqid')->move($this->root.'image', true, false);
        if ($info) {
            $people = array("ico", "png");
            if (!in_array($info->getExtension(),$people)) {
                $data = $this->request->param();
                if (!empty($data['width']) && !empty($data['height'])) {
                    $image = Image::open($this->root.'image/' . $info->getSaveName());
                    $image->thumb($data['width'], $data['height'])->save($this->root.'image/' . $info->getSaveName());
                }
            }
            echo $info->getSaveName();
        } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }


    /**
     * 文件
     */
    public function file()
    {

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('files');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->rule('uniqid')->move($this->root.'file', true, false);
        if ($info) {
            // 成功上传后 获取上传信息
            return json(array('size' => $info->getSize(), 'path' => $info->getSaveName()));
        } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }

    }

    public function uploaderMediaNewsImg()
    {
        $file = request()->file('file_upload');
        $info = $file->rule('uniqid')->validate(['ext' => 'jpg,png,gif,jpeg'])->move($this->root.'m');

        if ($info) {
            header('Content-Type:application/json; charset=utf-8');
            $res = [
                'code' => 1,
                'data' => request()->domain() . '/public/uploads/m/' . $info->getSaveName(),
                'message' => '上完成功'
            ];
            return json_encode($res);

        } else {
            // 上传失败获取错误信息
            $res = [
                'code' => 0,
                'data' => '',
                'message' => $info->getError()
            ];
            return json_encode($res);
        }
    }

    public function addkeyfile()
    {

        $file = request()->file('the_files') ? request()->file('the_files') : request()->file('files');

        $info = $file->rule('uniqid')->validate(['ext' => 'jpg,png,gif,jpeg,amr,mp4,mp3,rm,rmvb,wmv,avi,mpg,mpeg'])->move($this->root.'addkey');

        if ($info) {
            if (request()->file('files')) {
                return json(array('size' => $info->getSize(), 'path' => $info->getSaveName()));
            }
            echo $info->getSaveName();

        } else {
            echo $info->getError();
        }
    }

}
